package com.qububu.db.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qububu.db.dao.DineStaffDao;
import com.qububu.play.dineTogether.dto.StaffInfoDto;
import com.qububu.utils.ParamConverter;
import com.qububu.utils.SystemUtil;

@Repository
public class DineStaffDaoImpl implements DineStaffDao{
	
	private JdbcTemplate jdbcTemplate;
	
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	/**
	 * @功能 添加工作
	 * @操作表 staff
	 */
	public void doAddStaffInfo(StaffInfoDto staffInfo) {
		String sql = "insert into staff(staff_name, staff_explain, action_id) values(?, ?, ?)";
		
		int reActionId = ParamConverter.convertToInteger(SystemUtil.decrypt(staffInfo.getActionId()));

		Object[] addStaff = {
				staffInfo.getStaffName(),
				staffInfo.getExplain(),
				reActionId
		};
		
		jdbcTemplate.update(sql, addStaff);
	}

	public void modifyStaff(StaffInfoDto staffInfo) {
		String sql = "update staff set staff_name=?, staff_explain=? where staff_id = ?";
		
		int staffId = ParamConverter.convertToInteger(SystemUtil.decrypt(staffInfo.getStaffId()));
		
		Object[] data = {staffInfo.getStaffName(), staffInfo.getExplain(), staffId};
		
		jdbcTemplate.update(sql, data);
	}
	
	public List<StaffInfoDto> getStaffListByActionId(int actionId) {
		List<StaffInfoDto> staffList = null;
		staffList = new ArrayList<StaffInfoDto>();
		String sql = "SELECT * FROM staff WHERE action_id = ?";
		List<Map<String, Object>> staffMap = jdbcTemplate.queryForList(sql, actionId);
		StaffInfoDto staffInfoDto = null;
		for (Map<String, Object> elem : staffMap) {
			staffInfoDto = new StaffInfoDto((Long)elem.get("staff_id"), (Long)elem.get("action_id"));
			staffInfoDto.setExplain(String.valueOf(elem.get("staff_explain")));
			staffInfoDto.setStaffName(String.valueOf(elem.get("staff_name")));
			staffList.add(staffInfoDto);
		}
		return staffList;
	}

	public boolean deleteStaff(int staffId) {
		String sql = "delete from staff where staff_id=?";
		
		if (jdbcTemplate.update(sql, staffId) == 1) {
			return true;
		}
		
		return false;
	}
}
